home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / unix.subproj / tli.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  15.4 KB  |  629 lines

  1. /* tli.c
  2.    Code to handle TLI connections.
  3.  
  4.    Copyright (C) 1992, 1993, 1994 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #if USE_RCS_ID
  29. const char tli_rcsid[] = "$Id: tli.c,v 1.4 1995/06/21 19:20:50 ian Rel $";
  30. #endif
  31.  
  32. #if HAVE_TLI
  33.  
  34. #include "sysdep.h"
  35. #include "uudefs.h"
  36. #include "uuconf.h"
  37. #include "conn.h"
  38. #include "system.h"
  39.  
  40. #include <errno.h>
  41.  
  42. #if HAVE_SYS_IOCTL_H
  43. #include <sys/ioctl.h>
  44. #endif
  45.  
  46. #if HAVE_TIUSER_H
  47. #include <tiuser.h>
  48. #else
  49. #if HAVE_XTI_H
  50. #include <xti.h>
  51. #else
  52. #if HAVE_SYS_TLI_H
  53. #include <sys/tli.h>
  54. #endif
  55. #endif
  56. #endif
  57.  
  58. #if HAVE_STROPTS_H
  59. #include <stropts.h>
  60. #endif
  61.  
  62. #if HAVE_FCNTL_H
  63. #include <fcntl.h>
  64. #else
  65. #if HAVE_SYS_FILE_H
  66. #include <sys/file.h>
  67. #endif
  68. #endif
  69.  
  70. #ifndef O_RDONLY
  71. #define O_RDONLY 0
  72. #define O_WRONLY 1
  73. #define O_RDWR 2
  74. #endif
  75.  
  76. #ifndef FD_CLOEXEC
  77. #define FD_CLOEXEC 1
  78. #endif
  79.  
  80. /* The arguments to t_alloca have two different names.  I want the
  81.    SVID ones, not the XPG3 ones.  */
  82. #ifndef T_BIND
  83. #define T_BIND T_BIND_STR
  84. #endif
  85. #ifndef T_CALL
  86. #define T_CALL T_CALL_STR
  87. #endif
  88.  
  89. /* Hopefully these externs will not cause any trouble.  This is how
  90.    they are shown in the SVID.  */
  91. extern int t_errno;
  92. extern char *t_errlist[];
  93. extern int t_nerr;
  94.  
  95. #ifndef HAVE_TIUSER_H
  96. #ifndef t_alloc
  97. extern pointer t_alloc ();
  98. #endif
  99. #endif
  100.  
  101. /* This code handles TLI connections.  It's Unix specific.  It's
  102.    largely based on code from Unix Network Programming, by W. Richard
  103.    Stevens.  */
  104.  
  105. /* Local functions.  */
  106. static const char *ztlierror P((void));
  107. static void utli_free P((struct sconnection *qconn));
  108. static boolean ftli_push P((struct sconnection *qconn));
  109. static boolean ftli_open P((struct sconnection *qconn, long ibaud,
  110.                 boolean fwait));
  111. static boolean ftli_close P((struct sconnection *qconn,
  112.                  pointer puuconf,
  113.                  struct uuconf_dialer *qdialer,
  114.                  boolean fsuccess));
  115. static boolean ftli_dial P((struct sconnection *qconn, pointer puuconf,
  116.                 const struct uuconf_system *qsys,
  117.                 const char *zphone,
  118.                 struct uuconf_dialer *qdialer,
  119.                 enum tdialerfound *ptdialer));
  120.  
  121. /* The command table for a TLI connection.  */
  122. static const struct sconncmds stlicmds =
  123. {
  124.   utli_free,
  125.   NULL, /* pflock */
  126.   NULL, /* pfunlock */
  127.   ftli_open,
  128.   ftli_close,
  129.   ftli_dial,
  130.   fsysdep_conn_read,
  131.   fsysdep_conn_write,
  132.   fsysdep_conn_io,
  133.   NULL, /* pfbreak */
  134.   NULL, /* pfset */
  135.   NULL, /* pfcarrier */
  136.   fsysdep_conn_chat,
  137.   NULL /* pibaud */
  138. };
  139.  
  140. /* Get a TLI error string.  */
  141.  
  142. static const char *
  143. ztlierror ()
  144. {
  145.   if (t_errno == TSYSERR)
  146.     return strerror (errno);
  147.   if (t_errno < 0 || t_errno >= t_nerr)
  148.     return "Unknown TLI error";
  149.   return t_errlist[t_errno];
  150.  
  151. /* Initialize a TLI connection.  This may be called with qconn->qport
  152.    NULL, when opening standard input as a TLI connection.  */
  153.  
  154. boolean
  155. fsysdep_tli_init (qconn)
  156.      struct sconnection *qconn;
  157. {
  158.   struct ssysdep_conn *q;
  159.  
  160.   q = (struct ssysdep_conn *) xmalloc (sizeof (struct ssysdep_conn));
  161.   q->o = -1;
  162.   q->ord = -1;
  163.   q->owr = -1;
  164.   q->zdevice = NULL;
  165.   q->iflags = -1;
  166.   q->iwr_flags = -1;
  167.   q->fterminal = FALSE;
  168.   q->ftli = TRUE;
  169.   q->ibaud = 0;
  170.  
  171.   qconn->psysdep = (pointer) q;
  172.   qconn->qcmds = &stlicmds;
  173.   return TRUE;
  174. }
  175.  
  176. /* Free a TLI connection.  */
  177.  
  178. static void
  179. utli_free (qconn)
  180.      struct sconnection *qconn;
  181. {
  182.   xfree (qconn->psysdep);
  183. }
  184.  
  185. /* Push all desired modules onto a TLI stream.  If the user requests a
  186.    STREAMS connection without giving a list of modules, we just push
  187.    tirdwr.  If the I_PUSH ioctl is not defined on this system, we just
  188.    ignore any list of modules.  */
  189.  
  190. static boolean
  191. ftli_push (qconn)
  192.      struct sconnection *qconn;
  193. {
  194. #ifdef I_PUSH
  195.  
  196.   struct ssysdep_conn *qsysdep;
  197.  
  198.   qsysdep = (struct ssysdep_conn *) qconn->psysdep;
  199.  
  200.   if (qconn->qport->uuconf_u.uuconf_stli.uuconf_pzpush != NULL)
  201.     {
  202.       char **pz;
  203.  
  204.       for (pz = qconn->qport->uuconf_u.uuconf_stli.uuconf_pzpush;
  205.        *pz != NULL;
  206.        pz++)
  207.     {
  208.       if (ioctl (qsysdep->o, I_PUSH, *pz) < 0)
  209.         {
  210.           ulog (LOG_ERROR, "ioctl (I_PUSH, %s): %s", *pz,
  211.             strerror (errno));
  212.           return FALSE;
  213.         }
  214.     }
  215.     }
  216.   else if (qconn->qport->uuconf_u.uuconf_stli.uuconf_fstream)
  217.     {
  218.       if (ioctl (qsysdep->o, I_PUSH, "tirdwr") < 0)
  219.     {
  220.       ulog (LOG_ERROR, "ioctl (I_PUSH, tirdwr): %s",
  221.         strerror (errno));
  222.       return FALSE;
  223.     }
  224.     }
  225.  
  226.   /* If we have just put the connection into stream mode, we must turn
  227.      off the TLI flag to avoid using TLI calls on it.  */
  228.   if (qconn->qport->uuconf_u.uuconf_stli.uuconf_fstream)
  229.     qsysdep->ftli = FALSE;
  230.  
  231. #endif /* defined (I_PUSH) */
  232.   
  233.   return TRUE;
  234. }
  235.  
  236. /* Open a TLI connection.  If the fwait argument is TRUE, we are
  237.    running as a server.  Otherwise we are just trying to reach another
  238.    system.  */
  239.  
  240. static boolean
  241. ftli_open (qconn, ibaud, fwait)
  242.      struct sconnection *qconn;
  243.      long ibaud;
  244.      boolean fwait;
  245. {
  246.   struct ssysdep_conn *qsysdep;
  247.   const char *zdevice;
  248.   char *zfreedev;
  249.   const char *zservaddr;
  250.   char *zfreeaddr;
  251.   uid_t ieuid;
  252.   boolean fswap;
  253.   struct t_bind *qtbind;
  254.   struct t_call *qtcall;
  255.  
  256.   /* Unlike most other device types, we don't bother to call
  257.      ulog_device here, because fconn_open calls it with the name of
  258.      the port anyhow.  */
  259.  
  260.   qsysdep = (struct ssysdep_conn *) qconn->psysdep;
  261.  
  262.   zdevice = qconn->qport->uuconf_u.uuconf_stli.uuconf_zdevice;
  263.   if (zdevice == NULL)
  264.     zdevice = qconn->qport->uuconf_zname;
  265.  
  266.   zfreedev = NULL;
  267.   if (*zdevice != '/')
  268.     {
  269.       zfreedev = zbufalc (sizeof "/dev/" + strlen (zdevice));
  270.       sprintf (zfreedev, "/dev/%s", zdevice);
  271.       zdevice = zfreedev;
  272.     }
  273.  
  274.   /* If we are acting as a server, swap to our real user ID before
  275.      calling t_open.  This will permit the server to use privileged
  276.      TCP ports when invoked by root.  We only swap if our effective
  277.      user ID is not root, so that the program can also be made suid
  278.      root in order to get privileged ports when invoked by anybody.  */
  279.   fswap = fwait && geteuid () != 0;
  280.   if (fswap)
  281.     {
  282.       if (! fsuser_perms (&ieuid))
  283.     {
  284.       ubuffree (zfreedev);
  285.       return FALSE;
  286.     }
  287.     }
  288.  
  289.   qsysdep->o = t_open (zdevice, O_RDWR, (struct t_info *) NULL);
  290.   if (qsysdep->o < 0)
  291.     {
  292.       if (fswap)
  293.     (void) fsuucp_perms ((long) ieuid);
  294.       ulog (LOG_ERROR, "t_open (%s): %s", zdevice, ztlierror ());
  295.       ubuffree (zfreedev);
  296.       return FALSE;
  297.     }
  298.  
  299.   if (fcntl (qsysdep->o, F_SETFD,
  300.          fcntl (qsysdep->o, F_GETFD, 0) | FD_CLOEXEC) < 0)
  301.     {
  302.       if (fswap)
  303.     (void) fsuucp_perms ((long) ieuid);
  304.       ulog (LOG_ERROR, "fcntl (FD_CLOEXEC): %s", strerror (errno));
  305.       ubuffree (zfreedev);
  306.       (void) t_close (qsysdep->o);
  307.       qsysdep->o = -1;
  308.       return FALSE;
  309.     }
  310.  
  311.   qsysdep->iflags = fcntl (qsysdep->o, F_GETFL, 0);
  312.   if (qsysdep->iflags < 0)
  313.     {
  314.       if (fswap)
  315.     (void) fsuucp_perms ((long) ieuid);
  316.       ulog (LOG_ERROR, "fcntl: %s", strerror (errno));
  317.       ubuffree (zfreedev);
  318.       (void) t_close (qsysdep->o);
  319.       qsysdep->o = -1;
  320.       return FALSE;
  321.     }
  322.  
  323.   /* We save our process ID in the qconn structure.  This is checked
  324.      in ftli_close.  */
  325.   qsysdep->ipid = getpid ();
  326.  
  327.   /* If we aren't waiting for a connection, we can bind to any local
  328.      address, and then we're finished.  */
  329.   if (! fwait)
  330.     {
  331.       /* fswap is known to be FALSE here.  */
  332.       ubuffree (zfreedev);
  333.       if (t_bind (qsysdep->o, (struct t_bind *) NULL,
  334.           (struct t_bind *) NULL) < 0)
  335.     {
  336.       ulog (LOG_ERROR, "t_bind: %s", ztlierror ());
  337.       (void) t_close (qsysdep->o);
  338.       qsysdep->o = -1;
  339.       return FALSE;
  340.     }
  341.       return TRUE;
  342.     }
  343.  
  344.   /* Run as a server and wait for a new connection.  The code in
  345.      uucico.c has already detached us from our controlling terminal.
  346.      From this point on if the server gets an error we exit; we only
  347.      return if we have received a connection.  It would be more robust
  348.      to respawn the server if it fails; someday.  */
  349.   qtbind = (struct t_bind *) t_alloc (qsysdep->o, T_BIND, T_ALL);
  350.   if (qtbind == NULL)
  351.     {
  352.       if (fswap)
  353.     (void) fsuucp_perms ((long) ieuid);
  354.       ulog (LOG_FATAL, "t_alloc (T_BIND): %s", ztlierror ());
  355.     }
  356.  
  357.   zservaddr = qconn->qport->uuconf_u.uuconf_stli.uuconf_zservaddr;
  358.   if (zservaddr == NULL)
  359.     {
  360.       if (fswap)
  361.     (void) fsuucp_perms ((long) ieuid);
  362.       ulog (LOG_FATAL, "Can't run as TLI server; no server address");
  363.     }
  364.  
  365.   zfreeaddr = zbufcpy (zservaddr);
  366.   qtbind->addr.len = cescape (zfreeaddr);
  367.   if (qtbind->addr.len > qtbind->addr.maxlen)
  368.     {
  369.       if (fswap)
  370.     (void) fsuucp_perms ((long) ieuid);
  371.       ulog (LOG_FATAL, "%s: TLI server address too long (max %d)",
  372.         zservaddr, qtbind->addr.maxlen);
  373.     }
  374.   memcpy (qtbind->addr.buf, zfreeaddr, qtbind->addr.len);
  375.   ubuffree (zfreeaddr);
  376.  
  377.   qtbind->qlen = 5;
  378.  
  379.   if (t_bind (qsysdep->o, qtbind, (struct t_bind *) NULL) < 0)
  380.     {
  381.       if (fswap)
  382.     (void) fsuucp_perms ((long) ieuid);
  383.       ulog (LOG_FATAL, "t_bind (%s): %s", zservaddr, ztlierror ());
  384.     }
  385.  
  386.   if (fswap)
  387.     {
  388.       if (! fsuucp_perms ((long) ieuid))
  389.     ulog (LOG_FATAL, "Could not swap back to UUCP user permissions");
  390.     }
  391.  
  392.   (void) t_free ((pointer) qtbind, T_BIND);
  393.  
  394.   qtcall = (struct t_call *) t_alloc (qsysdep->o, T_CALL, T_ALL);
  395.   if (qtcall == NULL)
  396.     ulog (LOG_FATAL, "t_alloc (T_CALL): %s", ztlierror ());
  397.  
  398.   while (! FGOT_SIGNAL ())
  399.     {
  400.       int onew;
  401.       pid_t ipid;
  402.  
  403.       DEBUG_MESSAGE0 (DEBUG_PORT,
  404.               "ftli_open: Waiting for connections");
  405.  
  406.       if (t_listen (qsysdep->o, qtcall) < 0)
  407.     ulog (LOG_FATAL, "t_listen: %s", ztlierror ());
  408.  
  409.       onew = t_open (zdevice, O_RDWR, (struct t_info *) NULL);
  410.       if (onew < 0)
  411.     ulog (LOG_FATAL, "t_open (%s): %s", zdevice, ztlierror ());
  412.       
  413.       if (fcntl (onew, F_SETFD,
  414.          fcntl (onew, F_GETFD, 0) | FD_CLOEXEC) < 0)
  415.     ulog (LOG_FATAL, "fcntl (FD_CLOEXEC): %s", strerror (errno));
  416.  
  417.       if (t_bind (onew, (struct t_bind *) NULL, (struct t_bind *) NULL) < 0)
  418.     ulog (LOG_FATAL, "t_bind: %s", ztlierror ());
  419.  
  420.       if (t_accept (qsysdep->o, onew, qtcall) < 0)
  421.     {
  422.       /* We may have received a disconnect.  */
  423.       if (t_errno != TLOOK)
  424.         ulog (LOG_FATAL, "t_accept: %s", ztlierror ());
  425.       if (t_rcvdis (qsysdep->o, (struct t_discon *) NULL) < 0)
  426.         ulog (LOG_FATAL, "t_rcvdis: %s", ztlierror ());
  427.       (void) t_close (onew);
  428.       continue;
  429.     }
  430.  
  431.       DEBUG_MESSAGE0 (DEBUG_PORT,
  432.               "ftli_open: Got connection; forking");
  433.  
  434.       ipid = ixsfork ();
  435.       if (ipid < 0)
  436.     ulog (LOG_FATAL, "fork: %s", strerror (errno));
  437.       if (ipid == 0)
  438.     {
  439.       ulog_close ();
  440.  
  441.       (void) t_close (qsysdep->o);
  442.       qsysdep->o = onew;
  443.  
  444.       /* Push any desired modules.  */
  445.       if (! ftli_push (qconn))
  446.         _exit (EXIT_FAILURE);
  447.  
  448.       /* Now we fork and let our parent die, so that we become
  449.          a child of init.  This lets the main server code wait
  450.          for its child and then continue without accumulating
  451.          zombie children.  */
  452.       ipid = ixsfork ();
  453.       if (ipid < 0)
  454.         {
  455.           ulog (LOG_ERROR, "fork: %s", strerror (errno));
  456.           _exit (EXIT_FAILURE);
  457.         }
  458.           
  459.       if (ipid != 0)
  460.         _exit (EXIT_SUCCESS);
  461.  
  462.       ulog_id (getpid ());
  463.  
  464.       return TRUE;
  465.     }
  466.  
  467.       (void) t_close (onew);
  468.  
  469.       /* Now wait for the child.  */
  470.       (void) ixswait ((unsigned long) ipid, (const char *) NULL);
  471.     }
  472.  
  473.   /* We got a signal.  */
  474.   usysdep_exit (FALSE);
  475.  
  476.   /* Avoid compiler warnings.  */
  477.   return FALSE;
  478. }
  479.  
  480. /* Close the port.  */
  481.  
  482. /*ARGSUSED*/
  483. static boolean
  484. ftli_close (qconn, puuconf, qdialer, fsuccess)
  485.      struct sconnection *qconn;
  486.      pointer puuconf;
  487.      struct uuconf_dialer *qdialer;
  488.      boolean fsuccess;
  489. {
  490.   struct ssysdep_conn *qsysdep;
  491.   boolean fret;
  492.  
  493.   qsysdep = (struct ssysdep_conn *) qconn->psysdep;
  494.  
  495.   fret = TRUE;
  496.   if (qsysdep->o >= 0)
  497.     {
  498.       if (qsysdep->ftli)
  499.     {
  500.       if (t_close (qsysdep->o) < 0)
  501.         {
  502.           ulog (LOG_ERROR, "t_close: %s", ztlierror ());
  503.           fret = FALSE;
  504.         }
  505.     }
  506.       else
  507.     {
  508.       if (close (qsysdep->o) < 0)
  509.         {
  510.           ulog (LOG_ERROR, "close: %s", strerror (errno));
  511.           fret = FALSE;
  512.         }
  513.     }
  514.  
  515.       qsysdep->o = -1;
  516.     }
  517.  
  518.   /* If the current pid is not the one we used to open the port, then
  519.      we must have forked up above and we are now the child.  In this
  520.      case, we are being called from within the fendless loop in
  521.      uucico.c.  We return FALSE to force the loop to end and the child
  522.      to exit.  This should be handled in a cleaner fashion.  */
  523.   if (qsysdep->ipid != getpid ())
  524.     fret = FALSE;
  525.  
  526.   return fret;
  527. }
  528.  
  529. /* Dial out on a TLI port, so to speak: connect to a remote computer.  */
  530.  
  531. /*ARGSUSED*/
  532. static boolean
  533. ftli_dial (qconn, puuconf, qsys, zphone, qdialer, ptdialerfound)
  534.      struct sconnection *qconn;
  535.      pointer puuconf;
  536.      const struct uuconf_system *qsys;
  537.      const char *zphone;
  538.      struct uuconf_dialer *qdialer;
  539.      enum tdialerfound *ptdialerfound;
  540. {
  541.   struct ssysdep_conn *qsysdep;
  542.   char **pzdialer;
  543.   const char *zaddr;
  544.   struct t_call *qtcall;
  545.   char *zescape;
  546.  
  547.   qsysdep = (struct ssysdep_conn *) qconn->psysdep;
  548.  
  549.   *ptdialerfound = DIALERFOUND_FALSE;
  550.  
  551.   pzdialer = qconn->qport->uuconf_u.uuconf_stli.uuconf_pzdialer;
  552.   if (*pzdialer == NULL)
  553.     pzdialer = NULL;
  554.  
  555.   /* If the first dialer is "TLI" or "TLIS", we use the first token
  556.      (pzdialer[1]) as the address to connect to.  */
  557.   zaddr = zphone;
  558.   if (pzdialer != NULL
  559.       && (strcmp (pzdialer[0], "TLI") == 0
  560.       || strcmp (pzdialer[0], "TLIS") == 0))
  561.     {
  562.       if (pzdialer[1] == NULL)
  563.     ++pzdialer;
  564.       else
  565.     {
  566.       if (strcmp (pzdialer[1], "\\D") != 0
  567.           && strcmp (pzdialer[1], "\\T") != 0)
  568.         zaddr = pzdialer[1];
  569.       pzdialer += 2;
  570.     }
  571.     }
  572.   
  573.   if (zaddr == NULL)
  574.     {
  575.       ulog (LOG_ERROR, "No address for TLI connection");
  576.       return FALSE;
  577.     }
  578.  
  579.   qtcall = (struct t_call *) t_alloc (qsysdep->o, T_CALL, T_ADDR);
  580.   if (qtcall == NULL)
  581.     {
  582.       ulog (LOG_ERROR, "t_alloc (T_CALL): %s", ztlierror ());
  583.       return FALSE;
  584.     }
  585.  
  586.   zescape = zbufcpy (zaddr);
  587.   qtcall->addr.len = cescape (zescape);
  588.   if (qtcall->addr.len > qtcall->addr.maxlen)
  589.     {
  590.       ulog (LOG_ERROR, "%s: TLI address too long (max %d)", zaddr,
  591.         qtcall->addr.maxlen);
  592.       ubuffree (zescape);
  593.       return FALSE;
  594.     }
  595.   memcpy (qtcall->addr.buf, zescape, qtcall->addr.len);
  596.   ubuffree (zescape);
  597.  
  598.   if (t_connect (qsysdep->o, qtcall, (struct t_call *) NULL) < 0)
  599.     {
  600.       if (t_errno != TLOOK)
  601.     ulog (LOG_ERROR, "t_connect: %s", ztlierror ());
  602.       else
  603.     {
  604.       if (t_rcvdis (qsysdep->o, (struct t_discon *) NULL) < 0)
  605.         ulog (LOG_ERROR, "t_rcvdis: %s", ztlierror ());
  606.       else
  607.         ulog (LOG_ERROR, "Connection refused");
  608.     }
  609.       return FALSE;
  610.     }
  611.  
  612.   /* We've connected to the remote.  Push any desired modules.  */
  613.   if (! ftli_push (qconn))
  614.     return FALSE;      
  615.  
  616.   /* Handle the rest of the dialer sequence.  */
  617.   if (pzdialer != NULL && *pzdialer != NULL)
  618.     {
  619.       if (! fconn_dial_sequence (qconn, puuconf, pzdialer, qsys, zphone,
  620.                  qdialer, ptdialerfound))
  621.     return FALSE;
  622.     }
  623.  
  624.   return TRUE;
  625. }
  626.  
  627. #endif /* HAVE_TLI */
  628.